package com.roads.site.service.impl;

import com.roads.common.constant.Constants;
import com.roads.common.utils.DateUtils;
import com.roads.site.domain.SiteLabourStaffBank;
import com.roads.site.mapper.SiteLabourStaffBankMapper;
import com.roads.site.mapper.SiteLabourStaffMapper;
import com.roads.site.service.ISiteLabourStaffBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 劳务工-银行账户Service业务层处理
 * 
 * @author nhp
 * @date 2023-03-24
 */
@Service
public class SiteLabourStaffBankServiceImpl implements ISiteLabourStaffBankService {

    @Autowired
    private SiteLabourStaffBankMapper mapper;

    @Autowired
    private SiteLabourStaffMapper staffMapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 劳务工-银行账户主键
     * @return 劳务工-银行账户
     */
    @Override
    public SiteLabourStaffBank getModelById(Integer id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     * 
     * @param model 劳务工-银行账户
     * @return 劳务工-银行账户
     */
    @Override
    public List<SiteLabourStaffBank> selectModelList(SiteLabourStaffBank model) {
        return mapper.selectModelList(model);
    }

    /**
     * 根据身份证号查询银行账户
     *
     * @param idNo 身份证号
     * @return 劳务工-银行账户集合
     */
    @Override
    public List<SiteLabourStaffBank> selectModelListByIdNo(String idNo) {
        return mapper.selectModelListByIdNo(idNo);
    }

    /**
     * 新增
     * 
     * @param model 劳务工-银行账户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SiteLabourStaffBank model) {
        model.setCreateTime(DateUtils.getNowDate());
        model.setTop(Constants.YES);
        // 更新其他合同不置顶
        mapper.updateTop(model.getIdNo());
        // 新增的银行账户默认是置顶
        mapper.insert(model);
        // 同步劳务工表银行账户信息
        staffMapper.syncStaffBank(model.getIdNo(), model.getBankAccount(), model.getBankName(), model.getOpenBank());
        return 1;
    }

    /**
     * 修改
     * 
     * @param model 劳务工-银行账户
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(SiteLabourStaffBank model) {
        mapper.update(model);
        // 如果是置顶的，需要同步到劳务工表
        if (Constants.YES.equals(model.getTop())) {
            staffMapper.syncStaffBank(model.getIdNo(), model.getBankAccount(), model.getBankName(), model.getOpenBank());
        }
        return 1;
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            delete(ids[i]);
        }
        return 1;
    }

    /**
     * 删除
     * 
     * @param id 劳务工-银行账户主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Integer id) {
        SiteLabourStaffBank bank = mapper.getModelById(id);
        // 判断是否为最新的银行账户，如果是最新的银行账户，需要重置劳务工表的银行账户
        if (Constants.YES.equals(bank.getTop())) {
            staffMapper.resetStaffBank(bank.getIdNo());
        }
        mapper.delete(id);
        return 1;
    }

    /**
     * 设置银行账号默认使用
     * @param id 银行账户id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int setTop(Integer id) {
        SiteLabourStaffBank bank = mapper.getModelById(id);
        // 更新其他合同不置顶
        mapper.updateTop(bank.getIdNo());
        bank.setTop(Constants.YES);
        mapper.update(bank);
        // 同步劳务工表
        staffMapper.syncStaffBank(bank.getIdNo(), bank.getBankAccount(), bank.getBankName(), bank.getOpenBank());
        return 1;
    }
}
